import React from 'react';
import CssBaseline from '@material-ui/core/CssBaseline';
import Link from '@material-ui/core/Link';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';
import Box from '@material-ui/core/Box';
import { Button, Card, CardContent, Divider, List, ListItem, ListItemText } from '@material-ui/core';

function Copyright() {
  return (
    <Typography variant="body2" color="textSecondary" align="center">
      {'Copyright © '}
      <Link color="inherit" href="https://github.com/nullptrjzz">
        nullptrjzz
      </Link>{' '}
      {new Date().getFullYear()}
      {'.'}
    </Typography>
  );
}

const useStyles = makeStyles((theme) => ({
  card: {
    margin: '48px',
    padding: '16px'
  },
  list: {
    border: `1px solid ${theme.palette.divider}`,
    borderRadius: theme.shape.borderRadius,
    margin: '24px 0'
  },
  button: {
    marginBottom: '16px'
  }
}));

const logout = () => {
  fetch('/logout', {
    method: 'post'
  }).then(() => {
    if (localStorage.getItem('type') === null || localStorage.getItem('type') === '') {
      window.location.href = '/login/text';
    } else {
      window.location.href = '/login/' + localStorage.getItem('type');
    }
  });
}

const gotoVerify = (s) => {
  window.location.href = '/verify?secret=' + s;
}

export default function Result() {
  const classes = useStyles();
  const un = document.getElementById('un').innerHTML;
  const pwd = document.getElementById('pwd').innerHTML;
  const secret = document.getElementById('secret').innerHTML;

  return (
    <Container maxWidth="sm">
      <CssBaseline />
      <Card className={classes.card}>
        <CardContent>
          <Typography variant="h4">登录成功</Typography>
          <List className={classes.list} component="ul">
            <ListItem>
              <ListItemText primary="用户名" secondary={un} />
            </ListItem>
            <Divider component="li" />
            <ListItem>
              <ListItemText primary="密码" secondary={pwd} />
            </ListItem>
            <Divider component="li" />
            <ListItem>
              <ListItemText primary="Secret" secondary={secret} />
            </ListItem>
          </List>
          <Button
            className={classes.button}
            type="button"
            fullWidth
            variant="contained"
            color="primary"
            onClick={() => { gotoVerify(secret) }}>验证Secret</Button>
          <Button
            type="button"
            fullWidth
            variant="contained"
            color="secondary"
            onClick={logout}>注销</Button>
        </CardContent>
      </Card>
      <Box mt={8}>
        <Copyright />
      </Box>
    </Container>
  );
}